package acn.spring.domain;

import java.io.Serializable;

import javax.persistence.Embedded;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.validation.constraints.Min;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

@Entity
public class ProductOrder implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7449625824398915031L;

	@EmbeddedId
	private ProductOrderId pk;

	@Min(value = 1)
	private int quantity;

	@Embedded
	private Review review;

	public ProductOrder() {
	}

	public ProductOrder(Order order, Product product, int quantity) {
		setOrder(order);
		setProduct(product);
		setReview(new Review(0));
		this.quantity = quantity;
	}

	public ProductOrder(Order order, Product product) {
		this(order, product, 1);
	}

	public void setComment(String comment) {
		review.setComment(comment);
	}

	public void setStar(Integer star) {
		review.setStar(star);
	}

	public void setOrder(Order order) {
		getPk().setOrder(order);
	}

	public Order getOrder() {
		return pk.getOrder();
	}

	public void setProduct(Product product) {
		getPk().setProduct(product);
	}

	public Product getProduct() {
		return pk.getProduct();
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public ProductOrderId getPk() {
		if (pk == null) {
			pk = new ProductOrderId();
		}
		return pk;
	}

	public void setPk(ProductOrderId pk) {
		this.pk = pk;
	}

	public Review getReview() {
		if (review == null) {
			review = new Review();
		}
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!(obj instanceof ProductOrder))
			return false;

		ProductOrder that = (ProductOrder) obj;

		return new EqualsBuilder().append(getPk(), that.getPk()).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 31).append(getPk()).toHashCode();
	}
}
